package com.bomb.voice.feature.operationAccount;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.bomb.voice.R;
import com.bomb.voice.bean.QrCodeBean;
import com.bomb.voice.net.HttpSubscriber;
import com.bomb.voice.net.request.VoiceLoader;
import com.bomb.voice.utils.Img2AlbumUtil;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobInstallation;

public class RechargeActivity extends AppCompatActivity {


    @Bind(R.id.et_rechargeNum)
    EditText et_rechargeNum;
    @Bind(R.id.btn_preCreate)
    Button btn_preCreate;
    @Bind(R.id.btn_scan)
    Button btn_scan;
    @Bind(R.id.iv_pay_way)
    ImageView iv_pay_way;
    @Bind(R.id.activity_recharge)
    LinearLayout activity_recharge;
    String installationId = BmobInstallation.getCurrentInstallation().getInstallationId();
    private SVProgressHUD svProgressHUD;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge);
        ButterKnife.bind(this);
        svProgressHUD = new SVProgressHUD(this);


    }

    private void toAliPayScan() {
        try {
            //利用Intent打开支付宝
            //支付宝跳过开启动画打开扫码和付款码的url scheme分别是alipayqr://platformapi/startapp?saId=10000007
            //和alipayqr://platformapi/startapp?saId=20000056
            Uri uri = Uri.parse("alipayqr://platformapi/startapp?saId=10000007");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        } catch (Exception e) {
            //若无法正常跳转，在此进行错误处理
            Toast.makeText(this, "无法跳转到支付宝，请检查您是否安装了支付宝！", Toast.LENGTH_SHORT).show();
        }
    }

    @OnClick({R.id.btn_preCreate, R.id.btn_scan})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_preCreate:
                String trim = et_rechargeNum.getText().toString().trim();
                if (TextUtils.isEmpty(trim)) {
                    Toast.makeText(this, "请填写金额", Toast.LENGTH_SHORT).show();
                    return;
                }
                svProgressHUD.showWithStatus("下单中...");

                VoiceLoader voiceLoader = new VoiceLoader();
                voiceLoader.preRecharge(installationId, Float.parseFloat(trim))

                        .subscribe(new HttpSubscriber<QrCodeBean>() {

                            @Override
                            public void onNext(QrCodeBean qrCodeBean) {
                                Glide.with(RechargeActivity.this).load(qrCodeBean.getUri()).into(iv_pay_way);
                                Glide.with(RechargeActivity.this).load(qrCodeBean.getUri()).asBitmap()
                                        .into(new SimpleTarget<Bitmap>() {
                                            @Override
                                            public void onResourceReady(Bitmap templetBitmap, GlideAnimation anim) {
                                                // 第一次请求网络的时候使用 缓存数据
                                                Img2AlbumUtil img2AlbumUtil = new Img2AlbumUtil();
                                                img2AlbumUtil.saveImageToSysAlbum(RechargeActivity.this, templetBitmap, "voiceapp.png");
                                                Toast.makeText(RechargeActivity.this, "图片已经自动保存到相册,去支付宝-->相册 扫一扫即可付款", Toast.LENGTH_SHORT).show();

                                            }
                                        });

                                svProgressHUD.dismiss();

                            }

                            @Override
                            public void onError(Throwable throwable) {
                                super.onError(throwable);
                                Toast.makeText(RechargeActivity.this, "请求服务器失败", Toast.LENGTH_SHORT).show();
                                svProgressHUD.dismiss();

                            }
                        });
                break;
            case R.id.btn_scan:
                toAliPayScan();
                break;
        }
    }
}
